﻿
#region NameSpace
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Xml;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Xml.Xsl;
using FrameWorkMain;
#endregion

public partial class TreeViewMenu : System.Web.UI.UserControl
{
    #region Fields
    string connectionString = string.Empty;
    DataSet menuDS;
    FrameWorkMain.FrameWorkMain frameWork = new FrameWorkMain.FrameWorkMain();
    #endregion
    
    #region Methods

    /// <summary>
    /// Load TreeView Menu from Database
    /// </summary>
    /// <param name="role">Userrole</param>
    protected void PopulateMenu(string role)
    {
        try
        {
             string ff =string.Empty;
            string requestTypeId=string.Empty;
            menuDS = frameWork.GetMenu("");
            xmlDataSource.Data = menuDS.GetXml();
            //  this.TreeView1.DataSource = xmlDataSource;
            //  this.TreeView1.DataBind();
            //if (Request.Params["Sel"] != null)
            //{
            //    Session["Path"] = Request.Params["Sel"];
            //}
            //if (Request.Params["RequestType"] != null)
            //    requestTypeId = Request.Params["RequestType"];
            //if (TreeView1.SelectedNode != null)
            //    ff = TreeView1.SelectedNode.Text;
            if (Request.Params["Sel"] != null)
            {
                char splitter = ';';
                string path = Request.Params["Sel"].ToString();
                string[] splitid = path.Split(splitter);
                Session["Path"] = splitid[0];
                Session["requestTypeId"] = splitid[1];
            }
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Menu Loading Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }
    #endregion

    #region Events
    /// <summary>
    /// Load Menu
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    
    {
       // TreeView1.Attributes.Add("onMouseOver", "ClientSideCode();");
      //  connectionString = "Data Source=198.64.249.24,1092;Initial Catalog=eInternShip;User Id=bmjobmjo;Password=goodhopealways";
        connectionString=ConfigurationManager.ConnectionStrings["connectUserManager"].ToString();
        if (!Page.IsPostBack)
        {
            PopulateMenu("");
        }
      

    }


    protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
    {
       
        string childName = string.Empty;
        if( Session["ChildNode"]!=null)
            childName = Session["ChildNode"].ToString();

        if (Session["TreeViewState"] != null)
        {
            XmlElement element = (XmlElement)e.Node.DataItem;
           
            string selectedtNode = element.Attributes["Text"].Value;
            if (selectedtNode == Session["TreeViewState"].ToString())
            {

               // string dd = ((TreeView)sender).SelectedNode.ToolTip;
                e.Node.SelectAction = TreeNodeSelectAction.Expand;
                e.Node.PopulateOnDemand = false;
                e.Node.Expanded = true;
                return;
            }
            else if (selectedtNode == childName)
            {
              //  string ff = ((TreeView)sender).SelectedNode.Text;
                e.Node.SelectAction = TreeNodeSelectAction.Expand;
                e.Node.PopulateOnDemand = false;
                e.Node.Expanded = true;
                return;
            }
        }


        //XmlElement element = (XmlElement)e.Node.DataItem;
        //e.Node.Text = element.Attributes["Text"].Value;
        //if (e.Node.Text == "User")
        //{
        //    e.Node.SelectAction = TreeNodeSelectAction.Expand;
        //    e.Node.PopulateOnDemand = false;
        //    e.Node.Expanded = true;
        //    return;
        //}
    }

    protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
    {
        TreeView tree = (TreeView)sender;

        int depth = e.Node.Depth;
        int count=0;
        string currValue = string.Empty;
        if(e.Node.Parent!=null)
            count=e.Node.Parent.ChildNodes.Count;
        if (count>0)
        {
            currValue = Session["TreeViewState"].ToString().Trim();
            Session["ChildNode"] = e.Node.Value.Trim();
        }
        else
        {
             currValue = e.Node.Value.Trim();
        }
        Session["TreeViewState"] = currValue;



        //string currValue = e.Node.Value.Trim();
        //if (e.Node.Parent == null)
        //{
        //    foreach (TreeNode node in TreeView1.Nodes)
        //    {
        //        if (node.Value == currValue)
        //        {
        //            e.Node.PopulateOnDemand = false;
        //            node.Expand();
        //        }
        //    }
        //    return;
        //}
        //else
        //{
        //    TreeNode tnode = e.Node.Parent;
        //    foreach (TreeNode node in tnode.ChildNodes)
        //    {
        //        if (node.Value == e.Node.Value)
        //        {
        //            node.Expand();
        //        }
        //    }
        //}

    }
    #endregion


    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        TreeView tree = (TreeView)sender;
        tree.SelectedNode.Expand();
        // 
        if (tree.SelectedNode.Parent != null)
        {
            for (int i = 0; i < tree.SelectedNode.Parent.ChildNodes.Count; i++)
                tree.SelectedNode.Parent.ChildNodes[i].Expand();
        }
        else
        {
            for (int i = 0; i < tree.Nodes.Count; i++)
                tree.Nodes[i].Expand();
        }

        // TreeView1.SelectedNode.Expand();
    }
    protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        string ff =  ((TreeView)sender).SelectedNode.Text;
    }
}
